import Vue from 'vue'
import VeeValidate, {Validator} from 'vee-validate'
import zh from 'vee-validate/dist/locale/zh_CN' //引入中文文件

// 配置中文
Validator.addLocale(zh)

const config = {
  locale: 'zh_CN',
  errorBagName: 'errors', // change if property conflicts.
  fieldsBagName: 'fieldBags',
  delay: 0,
  messages: null,
  strict: true
};

Vue.use(VeeValidate,config);

// 自定义validate
const dictionary = {
  zh_CN: {
    messages: {
      email: () => '请输入正确的邮箱格式',
      required: (field) => "不能为空",
      numeric: (field) => "只能包含数字字符",
      max:(field,params)=>"长度大于"+params[0],
      min:(field,params)=>"长度小于"+params[0],
      between:(field,params)=>field+"必须在"+params[0]+'~'+params[1]+'之间'
    },
    attributes:{
      email:'邮箱',
      smsPhone: '手机',
      phone: '手机',
      no:'编号',
      name:'名称',
      userName:'用户名',
      addr12:'地址12位',
      setstartNo:'编号',
      GPSSerial:'GPS序列号'
    }
  }
};

Validator.updateDictionary(dictionary);

const validator = new Validator({
  runDate:'required',
  email: 'required|email',
  name: 'required|alpha_num|min:3',
  addr12:'required'
});

Validator.extend('phone', {
  messages: {
    zh_CN:field => '手机号格式不正确',
  },
  validate: value => {
    return /^\d{11,20}$/.test(value.trim())
  }
});
Validator.extend('smsPhone', {//工单接收号码可以输入多个
  messages: {
    zh_CN:field => '手机号格式不正确，多个手机号用,隔开',
  },
  validate: value => {
    return /^(1\d{10,12})((\,)(1\d{10,12}))*$/.test(value.trim())
  }
});

Validator.extend('addr12',{
  messages: {
    zh_CN:field =>'地址由(0-9A-Z)12位组成',
  },
  validate: value => {
    return /^[0-9A-Z]{12}$/.test(value.trim())
  }
})
Validator.extend('multiplication',{
  messages: {
    zh_CN:field =>'规格由（长*宽）或（长*宽*高）组成',
  },
  validate: value => {
    return /^(\d+(\.(\d+))?)((\*)(\d+(\.(\d+))?)){1,2}$/.test(value.trim())
  }
})
Validator.extend('lightNos',{
  messages: {
    zh_CN:field =>'灯头号由1,2,3组成',//1 1,2 ,1,2,3
  },
  validate: value => {
    return /^[1-3](\,[1-3]){0,2}$/.test(value.trim())
  }
})
Validator.extend('addr16',{
  messages: {
    zh_CN:field =>'地址由(0-9A-F)16位组成',
  },
  validate: value => {
    return /^[0-9A-F]{16}$/.test(value.trim())
  }
})
Validator.extend('GPSSerial',{
  messages: {
    zh_CN:field =>'GPS序列号为10位数组成',
  },
  validate: value => {
    return /^([0-9]{10}){0,1}$/.test(value.trim())
  }
})

Validator.extend('loginPwd', {
  messages: {
    zh_CN: filed => '密码长度8到12位，由字母、数字、特殊字符两种以上组成'
  },
  validate: value => {
    let regex = new RegExp('^(?![a-zA-Z]+$)(?![0-9]+$)(?![\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,30}$')
    return regex.test(value.trim())
  }
})

Validator.extend('ip', {
  messages: {
    zh_CN: filed => 'ip格式不正确'
  },
  validate: value => {
    let regex = new RegExp('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
    return regex.test(value.trim())
  }
})
Validator.extend('LngLat',{
  messages: {
    zh_CN:field =>'经度：0-180，纬度：0-90（小数最多保留7位，格式为：经纬;纬度）',
  },
  validate: value => {
    let lon = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,7})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,7}|180)$/
    let lat =  /^(\-|\+)?([0-8]?\d{1}\.\d{0,7}|90\.0{0,7}|[0-8]?\d{1}|90)$/
    return value.indexOf(';')!=-1&&lon .test(value.split(';')[0])&&lat .test(value.split(';')[1])
  }
})

